package com.wanmait.deliverymangementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.deliverymangementsystem.mapper.ReviewsMapper;
import com.wanmait.deliverymangementsystem.pojo.Orders;
import com.wanmait.deliverymangementsystem.pojo.Reviews;
import com.wanmait.deliverymangementsystem.pojo.Runner;
import com.wanmait.deliverymangementsystem.pojo.Tasks;
import com.wanmait.deliverymangementsystem.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

@Service
public class ReviewsServiceImpl implements ReviewsService {
    @Resource
    ReviewsMapper reviewsMapper;
    @Resource
    TaskService taskService;
    @Resource
    OrdersService ordersService;
    @Resource
    HistoOrderService histoOrderService;
    @Resource
    RunnerService runnerService;
    @Resource
    UsersService usersService;
     @Override
     @Transactional(rollbackFor = Exception.class)
    public void createReview(Reviews reviews) {
        reviewsMapper.insert(reviews);
     Orders orders= ordersService.findByTaskID(reviews.getTask().getId());
        ordersService.updateStatus(orders.getId(),4);
        histoOrderService.completeReviewByTaskId(reviews.getTask().getId());
         Runner runner =runnerService.findById(orders.getRunner().getId());
        float rat= runner.getRating()*runner.getCompletedOrderNumber();
        rat+=reviews.getScore();
        rat/=runner.getCompletedOrderNumber();
         DecimalFormat df = new DecimalFormat("#.00");
         String formattedNumber = df.format(rat);
         rat=Float.parseFloat(formattedNumber);
        runner.setRating(rat);
        runnerService.updateRatingById(runner);
    }

    @Override
    public List<Reviews> findGoodReviews() {
        return reviewsMapper.findAllByscore();
    }

    @Override
    public PageInfo<Reviews> findUserReviews(Integer page, Integer userID) {
        PageHelper.startPage(page,3);
        List<Reviews>reviewsList =reviewsMapper.selectByUserID(userID);

        return new PageInfo<>(reviewsList,3);
    }

    @Override
    public Reviews findReview(Integer id,Integer OrderId) {
                if (id==null){
         Orders orders =ordersService.findOrder(OrderId);
        Tasks tasks =taskService.findByID(orders.getTask().getId());
        return reviewsMapper.findByOrdersID(tasks.getId());
                }
                    return reviewsMapper.findByID(id);
    }

    @Override
    public PageInfo<Reviews> findAll(Integer page) {
         if (page==null)
             page=0;
        PageHelper.startPage(page,7);
        List<Reviews> reviewsList =reviewsMapper.findAll();
        for (Reviews reviews : reviewsList) {
            reviews.setTask(taskService.findByID(reviews.getTask().getId()));
            reviews.getTask().setRunner(runnerService.findById(reviews.getTask().getRunner().getId()));
            reviews.setUser(usersService.findUser(reviews.getUser().getId()));
            reviews.setOrders(ordersService.findOrder(ordersService.findByTaskID(reviews.getTask().getId()).getId()));
        }
         return new PageInfo<>(reviewsList,11);
    }
}
